﻿Shader "Unlit/Sequence"
{
    Properties
    {
        _MainTex ("RBG:颜色 A:透贴", 2D) = "white" {}
        _Opacity ("透明度", range(0, 1)) = 0.5
        _Sequence ("序列帧", 2d) = "gary" {}
        _RowCount ("行", int) = 1 
        _ColCount ("列", int) = 1 
        _Speed ("速度", int) = 1 

        _BottomTex ("RBG:颜色 A:透贴", 2D) = "gary" {}
        [HDR]_Color ("", color) = (1.0, 1.0, 1.0, 1.0)
    }
    SubShader
    {
        Tags { 
            "Queue" = "Transparent"
            "RenderType"="TransparentCutout"            // 改为 cutout
            "ForceNoShadowCasting" = "True"             // 关闭阴影投射
            "IgnoreProjector" = "True"                  // 不响应投射器
            }
        LOD 100

        Pass
        {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }

            Blend One OneMinusSrcAlpha               // Blend One/SrcAlpha OneMinusSrcAlpha
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0

            // 输入参数
            uniform sampler2D _MainTex;
            uniform float4 _MainTex_ST;
            uniform half _Opacity;

            struct VertexInput {
                float4 vertex : POSITION;
                float2 uv0 : TEXCOORD0;
            };

            struct VertexOutput {
                float4 pos : SV_POSITION;
                float2 uv0 : TEXCOORD0;
            };

            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.pos = UnityObjectToClipPos( v.vertex );
                o.uv0 = TRANSFORM_TEX(v.uv0, _MainTex);
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                half4 var_MainTex = tex2D(_MainTex, i.uv0);
                half opacity = var_MainTex.a;
                return half4(var_MainTex.rgb * opacity, opacity);
            }
            ENDCG
        }

        Pass 
        {
            Name "FORWARD_AD"
            TAGS {
                "LIGHTMODE"="FORWARDBASE"
            }

            Blend One One
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0
            
            uniform sampler2D _Sequence; uniform float4 _Sequence_ST; 
            uniform half _RowCount;
            uniform half _ColCount;
            uniform half _Speed;
            uniform half _Opacity;

            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float2 uv : TEXCOORD0;
            };

            struct VertexOutput {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                v.vertex.xyz += v.normal * 0.008;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _Sequence);
                float id = floor(_Time.z * _Speed);
                float idv = floor(id / _ColCount);
                float idu = id - idv * _ColCount;
                float stepU = 1.0 / _ColCount;
                float stepV = 1.0 / _RowCount;
                float2 initUV = o.uv * float2(stepU, stepV) + float2(0.0, stepV * (_RowCount - 1));
                o.uv = initUV + float2(idu * stepU, -idv * stepV);
                return o;
            }

            float4 frag(VertexOutput i) : COLOR {
                half4 var_Sequence = tex2D(_Sequence, i.uv);
                half opacity = var_Sequence.a * _Opacity;
                return half4(var_Sequence.rgb * opacity, opacity);
            }
            ENDCG

        }
        Pass 
        {
            Name "FORWARD_ADD"
            TAGS {
                "LIGHTMODE"="FORWARDBASE"
            }

            Blend One One
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0
            
            uniform sampler2D _BottomTex;
            uniform float4 _BottomTex_ST;
            uniform half _Opacity;
            uniform half4 _Color;

            struct VertexInput {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float4 color: COLOR;
            };

            struct VertexOutput {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float4 color: COLOR;
            };

            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _BottomTex);
                o.color = v.color;
                return o;
            }

            float4 frag(VertexOutput i) : COLOR {
                i.uv = i.uv - float2(0.5, 0.5);
                float theta = atan2(i.uv.y, i.uv.x);
                theta = theta / 3.1415926 * 0.5 + 0.5;
                float r = length(i.uv) + frac(_Time.x * 3);
                i.uv = float2(theta, r);



                half4 var_BottomTex = tex2D(_BottomTex, i.uv);
                half3 finalRGB = (1 - var_BottomTex.rgb) * _Color;
                half opacity = (1 - var_BottomTex.r) * _Opacity * i.color.r;
                return half4(finalRGB.rgb * opacity, opacity);
            }
            ENDCG

        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}
